﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class MemberManager
    {
        private readonly IBaseDAL<Member> _MemberDal;
        private readonly IBaseDAL<SysVerifyCode> _CodeDal;
        public MemberManager()
        {
            _MemberDal = new BaseDAL<Member>(new DB());
            _CodeDal = new BaseDAL<SysVerifyCode>(new DB());
        }

        public virtual IPaginate<MemberVD> GetListBy(MemberQD im)
        {
            var res = _MemberDal.FindList();
            if (!string.IsNullOrEmpty(im.Query))
            {
                res = res.Where(n => n.name.Contains(im.Query)||n.wechaname.Contains(im.Query));
            }
            if (!string.IsNullOrEmpty(im.Type))
            {
                res = res.Where(n => n.Type==im.Type);
            }


            var list = res.OrderByDescending(n => n.CreateDateTime).ToPaginate(im,n => new MemberVD().MapFrom(n));
            return list;
        }


        public void Create(MemberCUD im)
        {
            Member model = new Member();
            im.MapTo(model);
            _MemberDal.Create(model);
        }


        public void Update(MemberCUD im)
        {
            Member model = _MemberDal.Find(im.Id);
            model.name = im.name;
            model.UserName = im.UserName;
            model.PassWord = im.PassWord;
            _MemberDal.Update(model);
        }


        public MemberCUD Find(int id)
        {
            Member model = _MemberDal.Find(id);
            return new MemberCUD().MapFrom(model);
        }


        public Member Check(LoginD im)
        {
            Member model = _MemberDal.FindList(n=>n.UserName==im.UserName&&n.PassWord==im.PassWord).FirstOrDefault();
            return model;
        }

        public Member Register(RegisterD im) 
        {
            if (_CodeDal.Exist(n => n.Mobile == im.Mobile && n.VerifyCode == im.Code))
            {

                Member model = _MemberDal.Find(im.MemberId);
                model.Mobile = im.Mobile;
                var res=  _MemberDal.Update(model);
                return res;
            }
            else
            {
                throw new Exception("错误的验证码");
            }
        }

        public void Delete(int id)
        {
            Member model = _MemberDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _MemberDal.Delete(model);
        }


        public void LikeHouse(LikeD im)
        {
            Member model = _MemberDal.Find(im.MemberId);
            if (im.IsLike)
            {
                if (string.IsNullOrEmpty(model.LikeHouseIds))
                {
                    model.LikeHouseIds=im.HouseId.ToString();
                }
                else
                {
                    model.LikeHouseIds = model.LikeHouseIds + "," + im.HouseId.ToString();
                }

            }
            else
            {
                var ids = model.LikeHouseIds.Split(',').Where(n=>n!=im.HouseId.ToString()&&n!="");
                model.LikeHouseIds = string.Join(",", ids);
            }
            _MemberDal.Update(model);
        }
    }
}